/*
 * Project it.battlehorse.rcp.tools.dbbase
 * Created on Nov 21, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */ 
package it.battlehorse.rcp.tools.dbbase.preferences;

import it.battlehorse.rcp.tools.dbbase.DatasourceConfig;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;

/**
 * Defines the label provider for the table which contains the 
 * Datasource preferences
 * 
 * @author battlehorse
 * @since Nov 21, 2005
 */
public class DbTableLabelProvider implements ITableLabelProvider {
	
	private List<ILabelProviderListener> listeners =
			new ArrayList<ILabelProviderListener>();

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
	 */
	public Image getColumnImage(Object element, int columnIndex) {
		// no images
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
	 */
	public String getColumnText(Object element, int columnIndex) {
		DatasourceConfig cfg = (DatasourceConfig) element;
		switch (columnIndex) {
		case 0:
			return cfg.getName();
		case 1:
			return cfg.getDatabase().getName();
		case 2:
			return cfg.getConnectionUrl();
		case 3:
			return cfg.getUser();
		case 4:
			return convertToEchoChar(cfg.getPassword(),'*');
		default:
			return "invalid column " + columnIndex;						
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
	 */
	public void addListener(ILabelProviderListener listener) {
		listeners.add(listener);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
	 */
	public void dispose() {
		// nothing to dispose

	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
	 */
	public boolean isLabelProperty(Object element, String property) {
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
	 */
	public void removeListener(ILabelProviderListener listener) {
		listeners.remove(listener);

	}
	
	/**
	 * Converts a String into a set of echo characters
	 * 
	 * @param s the string to be converted
	 * @param echoChar the echo char
	 * @return the converted string
	 */
	private String convertToEchoChar(String s,char echoChar) {
		char[] res = new char[s.length()];
		Arrays.fill(res,echoChar);
		return new String(res);
	}

}
